/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import main.ServicesList;
import model.system.SystemReport;
import view.inventory.RekapStokFrm;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class ReportPoolFrm extends javax.swing.JFrame {

    /**
     * Creates new form ReportPoolFrm
     */
    public ReportPoolFrm() {
        initComponents();
    }
    
    public void execute(String modulname,boolean withVisible){
        loadMenu(modulname);                
        if (withVisible)
            this.setVisible(true);
    }
    
    private void loadMenu(String modulname){
        DefaultMutableTreeNode root = ServicesList.getSystemReportService().getTreeReport(modulname);
        DefaultTreeModel model = new DefaultTreeModel(root);
        treeMenu.setModel(model);
        
    }
    
    private void createTab(String reportName,Component frame){
         if (this.isExistTab(reportName)) {
             return;
         }
        if (frame==null) {
             tabMain.add(reportName, new JPanel());
         }
        else {
             tabMain.add(reportName,frame);
         }     
        int idx = tabMain.getTabCount()-1;
        tabMain.setSelectedIndex(idx);
        
       JPanel pnlTab = new JPanel(new GridBagLayout());
       pnlTab.setOpaque(false);
       JLabel lblLitle = new JLabel(reportName+" ");
       JButton btnClose = new JButton("x");
       ImageIcon ico = new ImageIcon(getClass().getResource("/no.png"));
        //Image  img = ico.getImage();
       JLabel lblClose = new JLabel(ico) ;//new ImageIcon("/images/no.png")
       lblClose.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
       GridBagConstraints gbc = new GridBagConstraints();
       gbc.gridx = 0;
       gbc.gridy = 0;
       gbc.weightx = 1;
       pnlTab.add(lblLitle,gbc);
       gbc.gridx++;
       gbc.gridx+=10;
       gbc.weightx = 1;
       pnlTab.add(lblClose,gbc);
       tabMain.setTabComponentAt(idx, pnlTab);
       
       lblClose.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                
                Component selected = tabMain.getSelectedComponent();
                if (selected!=null){
                    tabMain.remove(selected);
                }
            }
       });
       btnClose.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Component selected = tabMain.getSelectedComponent();
                if (selected!=null){
                    tabMain.remove(selected);
                }
            }
        } ); 
         
    }
      
      
      private boolean isExistTab(String id){
          boolean rs = false;
          for (int i=0;i<tabMain.getTabCount();i++){
              
              if (tabMain.getTitleAt(i).equals(id)){
                  tabMain.setSelectedIndex(i);
                  return true;
              }
          }
          return rs;
      }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        treeMenu = new javax.swing.JTree();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabMain = new javax.swing.JTabbedPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSplitPane1.setDividerSize(10);
        jSplitPane1.setOneTouchExpandable(true);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Reports");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Rekap Stok");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Kartu Stok");
        treeNode1.add(treeNode2);
        treeMenu.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        treeMenu.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeMenuValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(treeMenu);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jScrollPane2.setViewportView(tabMain);

        jSplitPane1.setRightComponent(jScrollPane2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void treeMenuValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeMenuValueChanged
        // TODO add your handling code here:
      DefaultMutableTreeNode node = (DefaultMutableTreeNode)treeMenu.getLastSelectedPathComponent();
      
      if (node==null)  return;
      
      Object nodeInfo = node.getUserObject();
      
      if (node.isLeaf()){
          if (nodeInfo instanceof ReportNode) {
            ReportNode rpt = (ReportNode)nodeInfo;
            //JOptionPane.showMessageDialog(null, rpt.getReportName());\
            switch (rpt.getReportId()){
                case SystemReport.REPORT_INVENTORY_REKAP_STOK :
                         // reportPoolFrm4 = new ReportPoolFrm();
                    RekapStokFrm rekapStokFrm = new RekapStokFrm();
                    rekapStokFrm.execute(false,0);
                    createTab(rpt.getReportName(), rekapStokFrm.getContentPane());
                     break;    
                default : JOptionPane.showMessageDialog(null, "Underconstruction");break;
            }
          }
      }
       //createTab(node., this);
        
    }//GEN-LAST:event_treeMenuValueChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
       
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ReportPoolFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane tabMain;
    private javax.swing.JTree treeMenu;
    // End of variables declaration//GEN-END:variables
}
